
package com.shop.cloud.mall.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.portal.annotation.ApiLogin;
import com.shop.cloud.mall.api.service.ShoppingCartService;
import com.shop.cloud.mall.common.entity.ShoppingCart;
import com.shop.cloud.common.portal.util.ThirdSessionHolder;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车
 *
 * @author
 * @date 2019-08-29 21:27:33
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/shoppingcart")
@Tag(description = "shoppingcart", name = "购物车API")
public class ShoppingCartController{

    private final ShoppingCartService shoppingCartService;

	/**
	 * 查询走缓存不能分页
	 * @return
	 */
	@Operation(summary = "查询走缓存不能分页")
    @GetMapping("/page")
	@ApiLogin
    public R getShoppingCartPage(ShoppingCart shoppingCart) {
		shoppingCart.setUserId(ThirdSessionHolder.getUserId());
		List<ShoppingCart> listShoppingCart = shoppingCartService.list2(shoppingCart);
		if(StrUtil.isNotBlank(shoppingCart.getShopId())){
			//过滤其他店铺数据
			listShoppingCart = listShoppingCart.stream().filter(s -> s.getShopId().equals(shoppingCart.getShopId())).collect(Collectors.toList());
		}
		return R.ok(new Page().setRecords(listShoppingCart).setTotal(listShoppingCart.size()));
    }

	/**
	 * 数量
	 * @return
	 */
	@Operation(summary = "查询数量")
	@GetMapping("/count")
	public R getShoppingCartCount(ShoppingCart shoppingCart) {
		String userId = ThirdSessionHolder.getUserId();
		if(StrUtil.isBlank(userId)){
			return R.ok(0);
		}
		shoppingCart.setUserId(userId);
		List<ShoppingCart> listShoppingCart = shoppingCartService.list2(shoppingCart);
		if(StrUtil.isNotBlank(shoppingCart.getShopId())){
			//过滤其他店铺数据
			listShoppingCart = listShoppingCart.stream().filter(s -> s.getShopId().equals(shoppingCart.getShopId())).collect(Collectors.toList());
		}
		return R.ok(listShoppingCart.size());
	}

	/**
	 * 加入购物车
	 * @param shoppingCart
	 * @return
	 */
	@Operation(summary = "加入购物车")
	@PostMapping
	@ApiLogin
	public R save(@RequestBody ShoppingCart shoppingCart){
		shoppingCart.setUserId(ThirdSessionHolder.getUserId());
		return R.ok(shoppingCartService.save(shoppingCart));
	}

	/**
	 * 修改购物车商品
	 * @param shoppingCart
	 * @return
	 */
	@Operation(summary = "修改购物车商品")
	@PutMapping
	@ApiLogin
	public R edit(@RequestBody ShoppingCart shoppingCart){
		shoppingCart.setUserId(ThirdSessionHolder.getUserId());
		return R.ok(shoppingCartService.updateById(shoppingCart));
	}

	/**
	 * 删除购物车商品数量
	 * @param ids
	 * @return
	 */
	@Operation(summary = "删除购物车商品数量")
	@PostMapping("/del")
	public R del(@RequestBody List<String> ids){
		return R.ok(shoppingCartService.removeByIds(ids));
	}


}
